﻿package org.soretro.portfolio.view.background 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	public class BorderFlicker extends Sprite {
		
		private var myBitmapDataObject			:BitmapData;
		private var timer						:Timer;
		public var myBitmap					:Bitmap;
		private var sW							:uint;
		private var sH							:uint;
		private var tempBarH					:Number;
		private var scrollCounter				:uint = 0;
		private var delay						:uint = 10;
		private var delayTime					:Number = 150;
		
		public function BorderFlicker() {
			
			sW = 1;
			sH = ( ( 1000 / 20 ) + 2 ) * 20;
			tempBarH = ( sH / ( sH / 20 ) );
			
			this.graphics.beginFill(0x666666);
			this.graphics.drawRect(0,0,sW,sH);
			this.graphics.endFill();
			
			myBitmapDataObject = new BitmapData( sW, sH, false, 0x00FFFF00 );
			
			myBitmap = new Bitmap(myBitmapDataObject);
			addChild(myBitmap);
			//myBitmap.y = -10;
			
			fillScreenData ();
			 
			timer = new Timer( delayTime, 1 );
			timer.addEventListener( TimerEvent.TIMER_COMPLETE, scrollBitmap );
//			timer.start();
			
			myBitmap.visible = false;
//			visible = false;
			width = 1;
		}
		
		public function stopLoading():void
		{
			myBitmap.visible = false;
			timer.stop();
			timer.removeEventListener( TimerEvent.TIMER_COMPLETE, scrollBitmap );
//			visible = false;
		}
		
		public function startLoading():void
		{
			myBitmap.visible = true;
			timer.addEventListener( TimerEvent.TIMER_COMPLETE, scrollBitmap );
			timer.start();
//			visible = true;
		}
		
		private function scrollBitmap( event:TimerEvent ):void {
			
			delay = 15;//Math.random()*5 + 10;
			
			if ( scrollCounter == delay ) {
				fillScreenHeader ();
			}
			if ( scrollCounter > delay*2 ) {
				scrollCounter = 0;
				fillScreenData ();
			}
			
			if ( scrollCounter > delay ) {
				scrollBitmapLoop( myBitmapDataObject, 0, uint( 10 - Math.random() * 4 ) );
			} else {
				scrollBitmapLoop( myBitmapDataObject, 0, uint( 40 - ( Math.random() * 52 ) ) );
			}
			
			//timer = new Timer( Math.random() * 40 + 100, 1 );
			timer = new Timer( delayTime, 1 );
			timer.start();
			timer.addEventListener( TimerEvent.TIMER_COMPLETE, scrollBitmap );
			
			scrollCounter++;
		}
		
		private function fillScreenHeader ():void {
			myBitmapDataObject.fillRect( new Rectangle( 0, 0, sW, sH ), 0x00FFFF );
			for ( var j:uint = 0; j < sH; j += ( tempBarH * 2 ) ) {
				myBitmapDataObject.fillRect( new Rectangle( 0, j, sW, tempBarH ), 0xFF0000 );
			}
		}
		
		private function fillScreenData ():void {
			myBitmapDataObject.fillRect( new Rectangle( 0, 0, sW, sH ), 0xFFFF00 );
			for ( var i:uint = 0; i < sH; i += Math.random() * 16 ) {
				myBitmapDataObject.fillRect( new Rectangle( 0, i, sW, Math.random() * 6 + 2 ), 0x0000FF );
				//myBitmapDataObject.scroll(0, uint( Math.random() * 16 ) );
			}
		}
		
		private function scrollBitmapLoop ( bitmapData:BitmapData, scrollX:int, scrollY:int ):void{
			// wrap values
			while(scrollX > bitmapData.width) scrollX -= bitmapData.width;
			while(scrollX < -bitmapData.width) scrollX += bitmapData.width;
			while(scrollY > bitmapData.height) scrollY -= bitmapData.height;
			while (scrollY < -bitmapData.height) scrollY += bitmapData.height;
			
			// the 4 edges of the bitmap
			var xPixels:int = Math.abs(scrollX), yPixels:int = Math.abs(scrollY);
			var rectR:Rectangle = new Rectangle(bitmapData.width-xPixels,0,xPixels,bitmapData.height);
			var rectL:Rectangle = new Rectangle(0,0,xPixels,bitmapData.height);
			var rectT:Rectangle = new Rectangle(0,0,bitmapData.width,yPixels);
			var rectB:Rectangle = new Rectangle(0,bitmapData.height-yPixels,bitmapData.width,yPixels);
			var pointL:Point = new Point(0,0);
			var pointR:Point = new Point(bitmapData.width-xPixels,0);
			var pointT:Point = new Point(0,0);
			var pointB:Point = new Point(0, bitmapData.height - yPixels);
			
			var tmp:BitmapData = new BitmapData(bitmapData.width, bitmapData.height, bitmapData.transparent, 0x000000);
			
			// copy column, scroll, paste
			scrollX > 0 ? tmp.copyPixels(bitmapData,rectR, pointL) : tmp.copyPixels(bitmapData,rectL, pointR);
			bitmapData.scroll(scrollX,0);
			scrollX > 0 ? bitmapData.copyPixels(tmp, rectL, pointL) : bitmapData.copyPixels(tmp, rectR, pointR);
			
			// copy row, scroll, paste
			scrollY > 0 ? tmp.copyPixels(bitmapData,rectB, pointT) : tmp.copyPixels(bitmapData,rectT, pointB);
			bitmapData.scroll(0,scrollY);
			scrollY > 0 ? bitmapData.copyPixels(tmp, rectT, pointT) : bitmapData.copyPixels(tmp, rectB, pointB);
			
			tmp.dispose();
		}
	}
}